from flask import Flask, request, jsonify, render_template
import json
import webbrowser

app = Flask(__name__)

FILE_PATH = 'json_info/main.json'
COL_PATH = 'json_info/col_info.json'


@app.route('/')
def index_flask():
    return render_template('index.html')


@app.route('/init_info', methods=['POST'])
def read_json():
    return_info = dict()

    with open(FILE_PATH, mode='r', encoding='utf-8') as f:
        return_info['names'] = json.loads(f.read())

    with open(COL_PATH, mode='r', encoding='utf-8') as f:
        return_info['col'] = json.loads(f.read())['col']

    return jsonify(return_info)


@app.route('/write_info', methods=['POST'])
def write_json_info():
    data = request.get_json()  # 获取JSON数据

    with open(FILE_PATH, mode='w', encoding='utf-8') as f:
        f.write(json.dumps(data['names'], ensure_ascii=False, indent=2))

    return jsonify({'info': '写入成功'})


if __name__ == '__main__':
    webbrowser.open("http://127.0.0.1:5000")
    app.run(debug=True, use_reloader=False, host='0.0.0.0')
